package com.pos.clothes.controller;


import com.github.pagehelper.PageInfo;
import com.pos.clothes.pojo.Category;
import com.pos.clothes.pojo.Color;
import com.pos.clothes.pojo.Size;
import com.pos.clothes.service.ColorService;
import com.pos.clothes.service.SizeService;
import com.pos.clothes.util.JsonData;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequiresPermissions("3")
@RequestMapping("/Size")
public class SizeController {
    @Autowired
    public SizeService service;

    @Autowired
    public Size size;

//    @RequestMapping("/getSize")
//    @ResponseBody
//    public PageInfo getColor(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) throws Exception {
//        PageInfo res = service.getSize(pageNum,pageSize);
//        return res;
//    }
    @RequestMapping("/getSize")
    @ResponseBody
    public List<Size> getSize(){

        List<Size> sizes = service.getSize();
        return sizes;
    }

    @RequestMapping(value = "/deleteSize")
    @ResponseBody
    public Object deleteSize(@RequestParam("idsize") Integer idsize) throws Exception {
        try {
            Integer res = service.deleteSize(idsize);
            if (res != 0) {
                return JsonData.buildSuccess(res, "删除成功");
            } else {
                return JsonData.buildError("删除失败");
            }
        }
        catch(Exception e) {
            return JsonData.buildError("删除失败！请先删除存在该尺码的规格商品！！");
        }


    }
    @RequestMapping(value = "/insertSize")
    @ResponseBody
    public Object insertSize(@RequestParam("size") String size){
        if(size==""){
            return JsonData.buildError("尺码名称不能为空");
        }
        List<Size> sizes = service.searchSize(size);
        if(!sizes.isEmpty()){
            return JsonData.buildError("尺码名称已经存在");
        }
        this.size.setSize(size);
        service.insertSize(this.size);
        return JsonData.buildSuccess(this.size.getIdsize(),"ok");

    }

    @RequestMapping(value = "/updateSize")
    @ResponseBody
    public Object updateSize(@RequestParam("idsize") int idsize,@RequestParam("size") String size){
        if(size==""){
            return JsonData.buildError("尺码名不能为空");
        }

        List<Size> sizes = service.searchSize(size);
        if(!sizes.isEmpty()) {
            if(sizes.get(0).getIdsize()!=idsize ) {
                return JsonData.buildError("尺码已存在");

            }

        }
        this.size.setIdsize(idsize);
        this.size.setSize(size);
        service.updateSize(this.size);
        return JsonData.buildSuccess(this.size.getIdsize(),"ok");
    }

}
